﻿package com.sampleProject 
{
	import com.fluidsoftwarellc.xon.XONLoader;
	import com.fluidsoftwarellc.xon.events.XONLoaderEvent;
	import com.sampleProject.dataModel.GroupOfPeople;
	import com.sampleProject.dataModel.Person;
	import fl.controls.Button;
	import fl.controls.TextArea;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Scott Tompkins
	 * FXON (Flash XML Object Notation) and Parser (http://code.google.com/p/as3-fxon/)
	 * Copyright (C) 2011  Scott C. Tompkins
	 * 
	 * This program is free software: you can redistribute it and/or modify
	 * it under the terms of the GNU General Public License as published by
	 * the Free Software Foundation, either version 3 of the License, or
	 * (at your option) any later version.
	 * 
	 * This program is distributed in the hope that it will be useful,
	 * but WITHOUT ANY WARRANTY; without even the implied warranty of
	 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	 * GNU General Public License for more details.
	 * 
	 * You should have received a copy of the GNU General Public License
	 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
	 */
	public class SampleProject extends MovieClip
	{
		
		
		//Stage Instance Ref's
		public var btnAuthor:Button;
		public var btnDeadPeople:Button;
		public var btnLooselyTypedObject:Button;
		public var taOutput:TextArea;
		
		
		public function SampleProject() 
		{
			//Wire up button event handlers
			btnAuthor.addEventListener(MouseEvent.CLICK, btnAuthorClicked);
			btnDeadPeople.addEventListener(MouseEvent.CLICK, btnDeadPeopleClicked);
			btnLooselyTypedObject.addEventListener(MouseEvent.CLICK, btnLooselyTypedObjectClicked);
		}
		
		
		
		private function writeToOutput(message:String):void
		{
			taOutput.text += message + "\n";
		}
		
		
		/* Loading author Object from FXON */
		private function btnAuthorClicked(evt:MouseEvent):void
		{
			var xonLoader:XONLoader = new XONLoader("data.xml", "author");
			xonLoader.addEventListener(XONLoaderEvent.LOADER_COMPLETE, authorLoaded);
			xonLoader.loadXON();			
		}
		
		private function authorLoaded(evt:XONLoaderEvent):void
		{
			var person:Person = evt.object as Person;
			writeToOutput(person.lastName + ", " + person.firstName + ": " + person.dob);
		}
		
		
		
		/* Loading deadPeople Object from FXON */
		private function btnDeadPeopleClicked(evt:MouseEvent):void
		{
			var xonLoader:XONLoader = new XONLoader("data.xml", "deadPeople");
			xonLoader.addEventListener(XONLoaderEvent.LOADER_COMPLETE, deadPeopleLoaded);
			xonLoader.loadXON();			
		}
		
		private function deadPeopleLoaded(evt:XONLoaderEvent):void
		{
			var groupOfPeople:GroupOfPeople = evt.object as GroupOfPeople;
			writeToOutput("Group Name: " + groupOfPeople.groupName);
			for each (var person:Person in groupOfPeople.people) 
			{
				writeToOutput(person.lastName + ", " + person.firstName + ": " + person.dob);
			}			
		}		
		

		
		/* Loading looselyTypedObject Object from FXON */
		private function btnLooselyTypedObjectClicked(evt:MouseEvent):void
		{
			var xonLoader:XONLoader = new XONLoader("data.xml", "looselyTypedObject");
			xonLoader.addEventListener(XONLoaderEvent.LOADER_COMPLETE, looselyTypedObjectLoaded);
			xonLoader.loadXON();			
		}		
		
		private function looselyTypedObjectLoaded(evt:XONLoaderEvent):void
		{
			writeToOutput("While I strongly discourage working with loosely typed objects, it is possible");
			var object:Object = evt.object;
			writeToOutput("isPossible? " + object.isPossible);
			writeToOutput("isWise?" + object.isWise);
		}				
		
		
		
		
		
		
		

	}

}